<?php

namespace App\Console\Commands;

use App\Model\Branch;
use App\Model\BranchTest;
use App\Model\Order;
use App\Model\Order1;
use App\Model\PushOrder;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Matrix\Exception;

class hfPush extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'hfPush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            try {
                //控制是否推单给下线
                $branch = Branch::where("id", 17)->first();
//            $branch = BranchTest::where("id",17)->first();
                if ($branch['tel'] == 1) {
                    $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_pushorder where status = '未处理'   order by id asc limit 1");
//                $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_order1 where status = '未处理'   order by id asc limit 1");
                    $num = Branch::where("tel", 1)->count();
                    if (isset($order[0])) {
                        $order1 = $order[0];
                        $order = (array)$order1;
                        if ($order['id'] % $num == 0) {
//                        if ($order['id'] % 1 == 0) {
                            $res = $this->hfPush($order['orderno'], $order['chargeaccount'], $order['remainingnumber']);
                            $info['ext'] = $res;
                            PushOrder::where("orderno", $order['orderno'])->update($info);
//                            Order1::where("orderno",$order['orderno'])->update($info);
                            $res = json_decode($res);
                            if ($res->code == 200) {
                                $info['status'] = '已处理';
                                $info['branch'] = '已推送,等待反馈';
                                $info['isok'] = 3;
                                $info['bid'] = 17;
                                $info['time'] = time();
                                PushOrder::where('orderno', $order['orderno'])->update($info);
                                Order::where('orderno', $order['orderno'])->update($info);
//                                Order1::where('orderno', $order['orderno'])->update($info);
                                $branch = Branch::where("id", 17)->first();
                                $orderInfo['num'] = $branch['num'] + 1;
                                $orderInfo['money'] = $branch['money'] + $order['remainingnumber'] * $order['num'];
                                Branch::where("id", $branch['id'])->update($orderInfo);
                            }
                        }
                    }
                }

                //更新下游的平均完成时间
                echo isset($order['id']) ? $order['id'] : 0;
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
            sleep(100000);
        }
    }

    public function hfPush($customer_order_no, $charge_account, $money)
    {
        $url = "http://120.25.254.213:8124/supply/order/orderCheck";
        $callBackUrl = 'http://112.124.24.221/admin/callback/hfAutoConfirmOrder';
        $key = "1dca207c784d3e352674dcb80fdb41be";
        $data1 = [
            "merchantId" => 201,
            "orderType" => 1,
            "mobile" => $charge_account,
            "tradeNoThird" => $customer_order_no,
            "amount" => number_format($money, 2),
            "notifyUrl" => $callBackUrl,
            "businessType" => 3
        ];
        $data = "merchantId=201&orderType=1&mobile=" . $charge_account . "&tradeNoThird=" . $customer_order_no . "&amount=" . number_format($money, 2) . "&notifyUrl=" . $callBackUrl . "&businessType=3";

        $str = implode("", $data1) . $key;

        $sign = md5($str);

        $data = $data . "&signstr=" . $sign;

        $res = $this->curlPost($url, $data);

        return $res;
    }

    public function curlPost($url, $post_data = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'charset=utf-8'));
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
